<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class City_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getAllCity($number, $offset, $keyword) {
        $return = array();
        $this->db->select('*')
                ->from('list_city')
                ->order_by('name')
                ->like('name', $keyword)
                ->limit($number, $offset);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getFullCity()
    {
        $return = array();
        $this->db->select('*')
                ->from('list_city')
                ->order_by('name');
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getInfo($id)
    {
        $return = array();
        $this->db->select('A.id, A.name')
                ->from('list_city A')
                ->where('A.id', $id);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    function count_all($keyword){
        if($keyword != ''){
            return $this->db->select('name')
                    ->from('list_city')
                    ->like('name', $keyword)
                    ->count_all_results();
        }
			return $this->db->count_all('list_city');
		}

    public function updateNameCity($Id, $data = array()) {
        if (!is_null($data)) {
            $result = $this->db->update('list_city', $data, array(
                'id' => $Id,
            ));
            return true;
        }
        return false;
    }
    
    public function updatePriceCity($typeGroup, $Id, $price_update) {
        $sql = "UPDATE `item_sim` SET `price` = `price_update`, `price_update` = `price_update` + $price_update WHERE $typeGroup = $Id";
        if ($this->db->query($sql))
            return true;
        return false;
    }
    
    public function checkCExists($Id)
    {
        $this->db->select('S.phone')
                ->from('item_sim S')
                ->join('list_city C', 'S.list_city_id = C.id', 'left')
                ->where('C.id', $Id);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }
    
    public function checkCityExists($list_city_id)
    {
        $this->db->select('*')
                ->from('list_city')
                ->where('id', $list_city_id);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }

    public function deleteNameCity($Id) {
        $this->db->delete('list_city', array('id' => $Id));
    }

    public function updateNameCityDeleteF($Id, $data = array()) {
        if (!is_null($data)) {
            $this->db->update('list_city', $data, array(
                'id' => $Id
            ));
        }
    }

    public function groupExists($name) {
        $this->db->select('*');
        $this->db->from('list_city');
        $this->db->where('name', $name);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }

    public function addNameCity($name) {
        $array = array(
//            'parent_id' => $parent_id,
            'name' => $name,
//            'name_en' => $name_en,
//            'href' => $href,
            'delete_flag' => '0'
        );
        return $this->db->insert('list_city', $array);
    }
    
     public function addAgentBatch($data) {
        return $this->db->insert_batch('list_city', $data);
    }
}

?>
